package com.shangyueshang.controller.basic;

import com.shangyueshang.model.SysUser;
import com.shangyueshang.model.SysUserRole;
import com.shangyueshang.request.IdDTO;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.request.PasswordDTO;
import com.shangyueshang.request.SysUserDTO;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.SysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author JokeyFeng
 * @date: 2022/1/4
 * @project: shangyueshang
 * @package: com.shangyueshang.controller
 * @comment:
 */
@RestController
@RequestMapping("/user")
public class UserController {
	
	@Resource
	private SysUserService sysUserService;
	
	/**
	 * 查询所有用户
	 *
	 * @return
	 */
	@GetMapping("/findAll")
	@PreAuthorize("hasAuthority('basic:user:view')")
	public HttpResult<List<SysUser>> findAll() {
		List<SysUser> list = sysUserService.list();
		list.sort(Comparator.comparing(SysUser::getCreateTime).reversed());
		return HttpResult.ok(list);
	}
	
	/**
	 * 保存用户
	 *
	 * @param sysUser
	 * @return
	 */
	@PostMapping("/save")
	@PreAuthorize("hasAuthority('basic:user:add') and hasAuthority('basic:user:edit')")
	public HttpResult<Void> save(@Valid @RequestBody SysUserDTO sysUser) {
		return sysUserService.save(sysUser);
	}
	
	/**
	 * 删除用户集合
	 *
	 * @param ids
	 * @return
	 */
	@PostMapping("/delete")
	@PreAuthorize("hasAuthority('basic:user:delete')")
	public HttpResult<Void> delete(@NotEmpty @RequestBody List<IdDTO> ids) {
		sysUserService.delete(ids.stream().map(IdDTO::getId).collect(Collectors.toList()));
		return HttpResult.ok();
	}
	
	/**
	 * 分页查询
	 *
	 * @param pageReq
	 * @return
	 */
	@PostMapping("/findPage")
	@PreAuthorize("hasAuthority('basic:user:view')")
	public HttpResult<PageResult<SysUser>> findPage(@RequestBody PageReq pageReq) {
		return HttpResult.ok(sysUserService.findPage(pageReq));
	}
	
	/**
	 * 根据名称查询
	 *
	 * @param username
	 * @return
	 */
	@GetMapping("/findByName")
	@PreAuthorize("hasAuthority('basic:user:view')")
	public HttpResult<SysUser> findByName(@NotBlank @RequestParam String username) {
		return HttpResult.ok(sysUserService.findByName(username));
	}
	
	/**
	 * 查询用户权限
	 *
	 * @param username
	 * @return
	 */
	@GetMapping("/findPermissions")
	@PreAuthorize("hasAuthority('basic:user:view')")
	public HttpResult<Set<String>> findPermissions(@NotBlank @RequestParam String username) {
		return HttpResult.ok(sysUserService.findPermissions(username));
	}
	
	/**
	 * 查询用户角色
	 *
	 * @param userId
	 * @return
	 */
	@GetMapping("/findUserRoles")
	@PreAuthorize("hasAuthority('basic:user:view')")
	public HttpResult<List<SysUserRole>> findUserRoles(@NotNull @RequestParam Long userId) {
		return HttpResult.ok(sysUserService.findUserRoles(userId));
	}
	
	/**
	 * 修改密码
	 *
	 * @return
	 */
	@PostMapping("/password/reset")
	public HttpResult<Void> resetPassword(@Valid @RequestBody PasswordDTO passwordDTO) {
		return sysUserService.resetPassword(passwordDTO);
	}
}
